package my.ts.code;

import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.AppContext;
import my.ts.code.objects.Root;
import android.app.Dialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoPlayer extends BaseActivity implements OnPreparedListener, ConfirmListener
// implements OnBufferingUpdateListener,
// OnPreparedListener, SurfaceHolder.Callback, OnCompletionListener,
// OnErrorListener
{
	// private boolean canContinue = false;

	AudioManager am;
	MediaPlayer mp;
	Thread progress;

	public final String A_SMS = "aSMS";
	public final String A_URL = "aURL";

	private Button download;

	private Dialog dialog;
	private VideoView vd;
	private Uri uri;

	// Root root;
	private TextView title;
	String tit, sid;
	Root root;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setCListener(this);
		myContext = this;
		init();
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				String link = root.getItems().get(0).getURL3GP();
				uri = Uri.parse(link.replace(" ", "%20"));
				vd.setVideoURI(uri);
				// thread.start();
			}
		}
	};

	@Override
	public void onResume() {

		super.onResume();
		AppContext.instance.session.setListener(this);

	}

	public void init() {
		Bundle b = getIntent().getExtras();
		tit = b.getString("title");
		sid = b.getString("sid");
		//
		AppContext.instance.session.getVideoDetails(sid);
		//
		setContentView(R.layout.vdplayer);
		download = (Button) findViewById(R.id.download);
		title = (TextView) findViewById(R.id.videotitle);
		title.setText(tit);
		vd = (VideoView) findViewById(R.id.VideoView);
		//
		vd.setOnPreparedListener(this);
		vd.setMediaController(new MediaController(this));

		download.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialogSelectSms(myContext, VideoPlayer.this, "Tùy chọn",
						"Bạn có đồng ý nhắn tin để $$$ clip", " clip",
						Config.CLIP_SYNTAX.getSyntax()
								+ root.getItems().get(0).getSMSDownloadCode(),
						Config.CLIP_SYNTAX.getNumber());

			}
		});

		showDialog();
	}

	protected void showDialog() {
		dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
		dialog.setContentView(R.layout.media_loading);
		dialog.setCancelable(true);
		dialog.show();
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub
		dialog.dismiss();
		vd.start();
	}

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub
		// System.out.println("ON DISPATCH");

		root = parse(data);
		if (root != null) {
			Message msg = handler.obtainMessage(1, root);
			handler.sendMessage(msg);
		} else {
			dialog.dismiss();
		}
	}

	@Override
	public void confirmListener(boolean done) {
		// TODO Auto-generated method stub
		
	}
}
